package com.weetop.cfw.base.presenter.imp

import com.weetop.cfw.base.presenter.PlantSalePublishPresenter
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.base.view.PlantSalePublishView
import com.weetop.cfw.bean.*
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.home_page.activity.PlantSalePublishActivity
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class PlantSalePublishPresenterImp(var plantSalePublishView: PlantSalePublishView? = null): PlantSalePublishPresenter {
    /**
     * 获取所有站点数据
     */
    override fun getAllSitesData(plantSalePublishActivity: PlantSalePublishActivity) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getAllSitesData(),plantSalePublishActivity,object:RxJavaCallBack<AllSitesBean>(){
            override fun onSuccess(t: AllSitesBean?) {
                plantSalePublishView?.getAllSitesDataSuccess(t)
            }
        })
    }

    /**
     * 获取建筑类型
     */
    override fun getBuildType(plantSalePublishActivity: PlantSalePublishActivity) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().kind(),plantSalePublishActivity,object:RxJavaCallBack<KindBean>(){
            override fun onSuccess(t: KindBean?) {
                plantSalePublishView?.getBuildKindDataSuccess(t)
            }
        })
    }

    /**
     * 发布出租出售信息
     */
    override fun publishLeaseSaleInformation(plantSalePublishActivity: PlantSalePublishActivity, publishMap: HashMap<String, String>) {
        plantSalePublishActivity.showLoadingDialog(plantSalePublishActivity,"正在发布，请稍等...")
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().publishLeaseSaleInformation(publishMap),plantSalePublishActivity,object:RxJavaCallBack<PublishBean>(){
            override fun onSuccess(t: PublishBean?) {
                plantSalePublishActivity.dismissLoadingDialog()
                plantSalePublishView?.leaseSaleInformationPublishSuccess(t)
            }

            override fun onRequestError(t: PublishBean?) {
                super.onRequestError(t)
                plantSalePublishActivity.dismissLoadingDialog()
            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                plantSalePublishActivity.dismissLoadingDialog()
            }
        })
    }

    /**
     * 根据城市名称获取站点信息
     */
    override fun getSiteInfoByCity(plantSalePublishActivity: PlantSalePublishActivity, cityName: String) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getCityIdByCityName(cityName),plantSalePublishActivity,object:RxJavaCallBack<CityIdBean>(){
            override fun onSuccess(t: CityIdBean?) {
                plantSalePublishView?.siteInfoGetSuccess(t)
            }

        })
    }

    /**
     * 根据城市名称获取其下面的县区信息
     */
    override fun getDistrictAndCountyData(plantSalePublishActivity: PlantSalePublishActivity, cityId: Int) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().quYu(cityId),plantSalePublishActivity,object:RxJavaCallBack<SiteBean>(){
            override fun onSuccess(t: SiteBean?) {
                plantSalePublishView?.getDistrictAndCountyDataSuccess(t)
            }
        })
    }

    /**
     * 获取行业数据
     */
    override fun getIndustryList(plantSalePublishActivity: PlantSalePublishActivity) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().generalCodeTable(1700),plantSalePublishActivity,object:RxJavaCallBack<GeneralCodeTableBean>(){
            override fun onSuccess(t: GeneralCodeTableBean?) {
                if(t != null){
                    plantSalePublishView?.industryDataGetSuccess(t)
                }
            }

            override fun onRequestError(t: GeneralCodeTableBean?) {
                super.onRequestError(t)
                if(t != null){
                    plantSalePublishView?.showNativeShortToast(t.message)
                }
            }

        })
    }

    /**
     * 获取面积归类数据
     */
    override fun areaClassificationList(plantSalePublishActivity: PlantSalePublishActivity) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().generalCodeTable(1100),plantSalePublishActivity,object:RxJavaCallBack<GeneralCodeTableBean>(){
            override fun onSuccess(t: GeneralCodeTableBean?) {
                if(t != null){
                    plantSalePublishView?.areaClassificationDataGetSuccess(t)
                }
            }

            override fun onRequestError(t: GeneralCodeTableBean?) {
                super.onRequestError(t)
                if(t != null){
                    plantSalePublishActivity.showNativeShortToast(t.message)
                }
            }

        })
    }

    /**
     * 上传图片
     */
    override fun uploadWorkshopDetailImage(plantSalePublishActivity: PlantSalePublishActivity, newHeadImgFile: String) {
        plantSalePublishActivity.showLoadingDialog(plantSalePublishActivity,"正在上传图片...")
        val file = File(newHeadImgFile)
        val image: RequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file)
        val headImgPart = MultipartBody.Part.createFormData("Imgfile", file.name, image)
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().uploadImage(multipartFile = headImgPart),plantSalePublishActivity,object:RxJavaCallBack<UploadImageBean>(){
            override fun onSuccess(t: UploadImageBean?) {
                if(t != null){
                    plantSalePublishActivity.dismissLoadingDialog()
                    plantSalePublishView?.uploadImageSuccess(t.imgurl)
                }
            }

            override fun onRequestError(t: UploadImageBean?) {
                super.onRequestError(t)
                plantSalePublishActivity.dismissLoadingDialog()
                plantSalePublishView?.showNativeShortToast(t?.message)
                plantSalePublishView?.uploadImageError()


            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                plantSalePublishActivity.dismissLoadingDialog()
                plantSalePublishView?.uploadImageError()

            }

        })
    }


    /**
     * 绑定
     */
    override fun attachView(baseView: BaseView) {
        plantSalePublishView = baseView as? PlantSalePublishView
    }

    /**
     * 解绑
     */
    override fun detachView() {
        plantSalePublishView = null
    }
}